import math
from pylab import *
import matplotlib.lines as mlines
import itertools
colors = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k',(0.5,0.5,0.5)])

LogRepre=False
U0='0'
dir='/home/jcfernandez/resultados/eps1/u'+U0+'/AMPL/W2.0/CONNECT/TOTAL/'
cuales=['0.05','0.1','0.2','0.5','1','2','5','10','20','40','60','100','200']
cuales=['0.05','1','10','20','40','60','100','200']
cabecera='dis-conA'
grp=[]
for ii in range(len(cuales)):
    x=[]
    y=[]
    for line in file(dir+cabecera+cuales[ii]+'.dat'):
        line=line.replace('\n','')
        res=[]
        val=line.split(' ')
        for v in val:
            if v!='':
                res.append(float(v))
        x.append(res[0])
        y.append(res[1])
    
    xxx=0.5*180./len(y)
    y2=[]
    fout=open(dir+'norm-'+cabecera+cuales[ii]+'.dat','w')
    suma=0.0
    for i in range(len(y)):
        factor=2.0/(math.cos((x[i]-xxx)*math.pi/180.)-math.cos((x[i]+xxx)*math.pi/180.))
        xa=x[i]
        ya=y[i]*factor
        y2.append(ya)
        if i>0:
            suma+=(x[i]-x[i-1])*(y2[i]+y2[i-1])*0.5
    
    
    for i in range(len(y2)):
        xa=x[i]
        y2[i]=y2[i]/suma
        ya=y2[i]/suma
        fout.write(str(xa)+' '+str(ya)+'\n')
    fout.close()
    color = colors.next()
    III=plot(x,y2,c=color,marker='o')
    grp.append(III)
    if LogRepre:
        yscale('log')

labels=[]
for v in cuales:
    labels.append(v+' %')
xlabel(r'$\theta $',fontsize=20)
ylabel(r'Probability',fontsize=14)
leg1=labels[0:5:1]
leg2=labels[5:9:1]
legend(tuple(grp[0:8:1]),tuple(labels[0:8:1]), shadow = True, loc = (0.88, 0.6))
#legend(tuple(grp[4:8:1]),tuple(labels[4:8:1]), shadow = True, loc = (0.0, 0.7))
#legend([grp[0]],['aaaa'],loc=1)
#legend([grp[2]],['bbb'],loc=2)
title("Probability of $\\theta$-connection for different $\\gamma_0$\nE1U"+U0+"W2.0")
if LogRepre:
    savefig(dir+'lognorm-'+cabecera+'.png', format='png')
else:
    savefig(dir+'norm-'+cabecera+'.png', format='png')
show()
